package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; //添加
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.emergency.domain.EmergencyPlan;
import com.ruoyi.emergency.service.IEmergencyPlanService;
import com.ruoyi.system.domain.GradedAlarmHistory;
import com.ruoyi.system.domain.GradedAlarmReal;
import com.ruoyi.system.domain.dto.GradedAlarmDto;
import com.ruoyi.system.mapper.GradedAlarmHistoryMapper;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.service.IGradedAlarmHistoryService;
import com.ruoyi.system.service.IGradedAlarmRealService;
//import com.ruoyi.warning.service.IAlarmLinkageControlService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.GradedAlarmMapper;
import com.ruoyi.system.domain.GradedAlarm;
import com.ruoyi.system.service.IGradedAlarmService;

/**
 * 分级报警设置Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-18
 */
@Service
public class GradedAlarmServiceImpl extends ServiceImpl<GradedAlarmMapper, GradedAlarm> implements IGradedAlarmService //修改
{
    @Autowired
    private GradedAlarmMapper gradedAlarmMapper;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Autowired
    private IGradedAlarmHistoryService gradedAlarmHistoryService;

    @Autowired
    private IGradedAlarmRealService gradedAlarmRealService;

    @Autowired
    private IEmergencyPlanService emergencyPlanService;

//    @Autowired
//    private IAlarmLinkageControlService iAlarmLinkageControlService;

    /**
     * 获取GradedAlarmDtoList
     *
     * @return 分级报警设置
     */
    public List<GradedAlarmDto> getGradedAlarmDtoList() {
        List<GradedAlarm> gradedAlarmList = gradedAlarmMapper.selectGradedAlarmList(new GradedAlarm());
        List<GradedAlarmDto> collect = gradedAlarmList.stream().map(gradedAlarm -> {
            GradedAlarmDto gradedAlarmDto = new GradedAlarmDto();
            BeanUtils.copyProperties(gradedAlarm, gradedAlarmDto);
            gradedAlarmDto.setGradedAlarmId(gradedAlarm.getGradedAlarmId());
            //报警区域
            gradedAlarmDto.setBaseStationName(sysDictDataMapper.selectDictLabel("alarm_area", gradedAlarm.getBaseStation()));
            //报警类型
            gradedAlarmDto.setAlarmTypeName(sysDictDataMapper.selectDictLabel("alarm_type", gradedAlarm.getAlarmType()));
            //报警等级
            gradedAlarmDto.setAlarmLevelName(sysDictDataMapper.selectDictLabel("alarm_level", gradedAlarm.getAlarmLevel()));
            //传感器类型
            gradedAlarmDto.setSensorTypeName(sysDictDataMapper.selectDictLabel("sensor_type", gradedAlarm.getSensorType()));
            //系统类型
            gradedAlarmDto.setSystemTypeName(sysDictDataMapper.selectDictLabel("system_type", gradedAlarm.getSystemType()));
            //报警时间
            gradedAlarmDto.setAlarmTime(new Date());
            return gradedAlarmDto;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 通过区域名称和传感器类型查询分级报警设置
     *
     * @return 分级报警设置
     */
    public List<GradedAlarmDto> selectGradedAlarmByBaseStationAndSensorType(String baseStation, String sensorType) {
        List<GradedAlarmDto> gradedAlarmDtoList = getGradedAlarmDtoList();
//        List<GradedAlarmDto> gradedAlarmDtoList1 = getGradedAlarmDtoList();
        List<GradedAlarmDto> gradedAlarmDtoList1 = new ArrayList<>();
        List<GradedAlarmDto> collect = gradedAlarmDtoList.stream().map(gradedAlarmDto -> {
            if (baseStation.equals(gradedAlarmDto.getBaseStationName()) && sensorType.equals(gradedAlarmDto.getSensorTypeName())) {
                gradedAlarmDtoList1.add(gradedAlarmDto);
            }
            return gradedAlarmDto;
        }).collect(Collectors.toList());
        return gradedAlarmDtoList1;
    }

    /**
     * 对报警分级进行处理
     *
     * @return 分级报警设置
     */
    @Override
    public GradedAlarmDto handleAlarmSpecial(String baseStation, String sensorType, String monitoringThreshold, String dataType) {
        List<GradedAlarmDto> gradedAlarmDtoList = selectGradedAlarmByBaseStationAndSensorType(baseStation, sensorType);
        GradedAlarmDto gradedAlarmDto1 = gradedAlarmDtoList.stream()
                .filter(gradedAlarmDto -> {
                    String alarmTypeName = gradedAlarmDto.getAlarmTypeName();
                    double a = Double.parseDouble(monitoringThreshold);
                    double b = Double.parseDouble(gradedAlarmDto.getMonitoringThreshold());
                    gradedAlarmDto.setAlarmValue(monitoringThreshold);
                    if (alarmTypeName.contains("过高")) {
                        if (alarmTypeName.contains(dataType)) {
//                            System.out.println("7k7k:" + gradedAlarmDto);
                            return a >= b;
                        }
                    } else if (alarmTypeName.contains("过低")) {
                        if (alarmTypeName.contains(dataType)) {
//                            System.out.println("8k8k:" + gradedAlarmDto);
                            return a <= b;
                        }
                    }
                    return false;
                })
                .sorted(Comparator.comparingInt(gradedAlarmDto -> Integer.parseInt(gradedAlarmDto.getAlarmLevel()))) // 转换为 int 类型进行排序
                .reduce((first, second) -> second)
                .orElse(null);

        //报警分级历史表处理
        if (gradedAlarmDto1 != null) {

            GradedAlarmHistory gradedAlarmHistory = new GradedAlarmHistory();
            BeanUtils.copyProperties(gradedAlarmDto1, gradedAlarmHistory);
            gradedAlarmHistoryService.insertGradedAlarmHistory(gradedAlarmHistory);
            //调用联动控制
//            iAlarmLinkageControlService.writeNodeValueByLinkageControl(gradedAlarmDto1);
        }
        //报警分级实时表处理
        if (gradedAlarmDto1 != null) {
            //存在报警
            GradedAlarmReal gradedAlarmReal = new GradedAlarmReal();
            BeanUtils.copyProperties(gradedAlarmDto1, gradedAlarmReal);
            gradedAlarmReal.setGradedAlarmId(gradedAlarmDto1.getGradedAlarmId());
            //查询该实时报警是否存在
            GradedAlarmReal gradedAlarmReal1 = gradedAlarmRealService.selectGradedAlarmRealByBaseStationAndSensorTypeAndAlarmType(baseStation, sensorType, dataType);
            if (gradedAlarmReal1 != null) {
                //该报警已存在需要修改
                gradedAlarmReal.setGradedAlramRealId(gradedAlarmReal1.getGradedAlramRealId());
                gradedAlarmRealService.updateGradedAlarmReal(gradedAlarmReal);
            } else {
                //该报警未存在
                gradedAlarmRealService.insertGradedAlarmReal(gradedAlarmReal);
            }
        } else {
            //该报警存在但报警值小于阈值，删除该报警
            //查询该实时报警是否存在
            GradedAlarmReal gradedAlarmReal1 = gradedAlarmRealService.selectGradedAlarmRealByBaseStationAndSensorTypeAndAlarmType(baseStation, sensorType, dataType);
            if (gradedAlarmReal1 != null) {
                //删除报警
                gradedAlarmRealService.deleteGradedAlarmRealByGradedAlramRealId(gradedAlarmReal1.getGradedAlramRealId());
            }
        }
        return gradedAlarmDto1;
    }

    /**
     * 对报警分级进行处理
     *
     * @return 分级报警设置
     */
    @Override
    public GradedAlarmDto handleAlarmNormal(String baseStation, String sensorType, String monitoringThreshold) {
        List<GradedAlarmDto> gradedAlarmDtoList = selectGradedAlarmByBaseStationAndSensorType(baseStation, sensorType);
//        System.out.println("gradedAlarmDtoList:" + gradedAlarmDtoList);
        GradedAlarmDto gradedAlarmDto1 = gradedAlarmDtoList.stream()
                .filter(gradedAlarmDto -> {
                    String alarmTypeName = gradedAlarmDto.getAlarmTypeName();
                    double a = Double.parseDouble(monitoringThreshold);
                    double b = Double.parseDouble(gradedAlarmDto.getMonitoringThreshold());
                    gradedAlarmDto.setAlarmValue(monitoringThreshold);
                    if (alarmTypeName.contains("过高")) {
//                        System.out.println("4399:" + gradedAlarmDto);
                        return a >= b;
                    } else if (alarmTypeName.contains("过低")) {
                        return a <= b;
                    }
                    return false;
                })
                .sorted(Comparator.comparingInt(gradedAlarmDto -> Integer.parseInt(gradedAlarmDto.getAlarmLevel()))) // 转换为 int 类型进行排序
                .reduce((first, second) -> second)
                .orElse(null);
        //报警分级历史表处理
        if (gradedAlarmDto1 != null) {
            GradedAlarmHistory gradedAlarmHistory = new GradedAlarmHistory();
            BeanUtils.copyProperties(gradedAlarmDto1, gradedAlarmHistory);
            gradedAlarmHistoryService.insertGradedAlarmHistory(gradedAlarmHistory);
            //调用联动控制
//            iAlarmLinkageControlService.writeNodeValueByLinkageControl(gradedAlarmDto1);
        }
        //报警分级实时表处理
        if (gradedAlarmDto1 != null) {
            //存在报警
            GradedAlarmReal gradedAlarmReal = new GradedAlarmReal();
            BeanUtils.copyProperties(gradedAlarmDto1, gradedAlarmReal);
            gradedAlarmReal.setGradedAlarmId(gradedAlarmDto1.getGradedAlarmId());
            //查询该实时报警是否存在
            GradedAlarmReal gradedAlarmReal1 = gradedAlarmRealService.selectGradedAlarmRealByBaseStationAndSensorType(baseStation, sensorType);
            if (gradedAlarmReal1 != null) {
                //该报警已存在需要修改
                gradedAlarmReal.setGradedAlramRealId(gradedAlarmReal1.getGradedAlramRealId());
                gradedAlarmRealService.updateGradedAlarmReal(gradedAlarmReal);
            } else {
                //该报警未存在
                gradedAlarmRealService.insertGradedAlarmReal(gradedAlarmReal);
            }
        } else {
            //该报警存在但报警值小于阈值，删除该报警
            //查询该实时报警是否存在
            GradedAlarmReal gradedAlarmReal1 = gradedAlarmRealService.selectGradedAlarmRealByBaseStationAndSensorType(baseStation, sensorType);
            if (gradedAlarmReal1 != null) {
                //删除报警
                gradedAlarmRealService.deleteGradedAlarmRealByGradedAlramRealId(gradedAlarmReal1.getGradedAlramRealId());
            }
        }
        return gradedAlarmDto1;
    }

    /**
     * 通过报警实时数据查询报警分级数据
     */
    @Override
    public GradedAlarmDto selectGradedAlarmDto(String baseStation, String sensorType, String alarmLevelName) {
        //获取报警分级dto
        List<GradedAlarmDto> gradedAlarmDtoList = getGradedAlarmDtoList();
        GradedAlarmDto gradedAlarmDto1 = new GradedAlarmDto();
        List<GradedAlarmDto> collect = gradedAlarmDtoList.stream().map(gradedAlarmDto -> {
//            System.out.println("20241125+gradedAlarmDto+" + gradedAlarmDto);
            if (baseStation.equals(gradedAlarmDto.getBaseStationName()) && sensorType.equals(gradedAlarmDto.getSensorTypeName()) && alarmLevelName.equals(gradedAlarmDto.getAlarmLevelName())) {
//                System.out.println("20241125+gradedAlarmDto+" + gradedAlarmDto);
                BeanUtils.copyProperties(gradedAlarmDto1,gradedAlarmDto);
            }
            return gradedAlarmDto;
        }).collect(Collectors.toList());
        return gradedAlarmDto1;
    }

    /**
     * 查询分级报警设置列表Dto
     */
    @Override
    public List<GradedAlarmDto> selectGradedAlarmListDto(GradedAlarm gradedAlarm) {
        List<GradedAlarm> gradedAlarmList = gradedAlarmMapper.selectGradedAlarmList(gradedAlarm);
//        System.out.println("8989:" + gradedAlarmList);
        List<GradedAlarmDto> gradedAlarmDtoList = new ArrayList<>();
        List<GradedAlarm> collect = gradedAlarmList.stream().map(gradedAlarm1 -> {
            GradedAlarmDto gradedAlarmDto = new GradedAlarmDto();
            BeanUtils.copyProperties(gradedAlarm1,gradedAlarmDto);
//            System.out.println("gradedAlarm1:" + gradedAlarm1);
//            System.out.println("gradedAlarmDto:" + gradedAlarmDto.getGradedAlarmId());
            if (gradedAlarm1.getEmergencyplanId() != null){
                EmergencyPlan emergencyPlan = emergencyPlanService.selectEmergencyPlanById(Long.valueOf(gradedAlarm1.getEmergencyplanId()));
                gradedAlarmDto.setEmergencyPlanName(emergencyPlan.getName());
            }
            gradedAlarmDtoList.add(gradedAlarmDto);
            return gradedAlarm;
        }).collect(Collectors.toList());
        return gradedAlarmDtoList;
    }


    /**
     * 查询分级报警设置
     *
     * @param gradedAlarmId 分级报警设置主键
     * @return 分级报警设置
     */
    @Override
    public GradedAlarm selectGradedAlarmByGradedAlarmId(String gradedAlarmId) {
        return gradedAlarmMapper.selectGradedAlarmByGradedAlarmId(gradedAlarmId);
    }

    /**
     * 查询分级报警设置列表
     *
     * @param gradedAlarm 分级报警设置
     * @return 分级报警设置
     */
    @Override
    public List<GradedAlarm> selectGradedAlarmList(GradedAlarm gradedAlarm) {
        return gradedAlarmMapper.selectGradedAlarmList(gradedAlarm);
    }

    /**
     * 新增分级报警设置
     *
     * @param gradedAlarm 分级报警设置
     * @return 结果
     */
    @Override
    public int insertGradedAlarm(GradedAlarm gradedAlarm) {
        gradedAlarm.setCreateTime(DateUtils.getNowDate());
        return gradedAlarmMapper.insertGradedAlarm(gradedAlarm);
    }

    /**
     * 修改分级报警设置
     *
     * @param gradedAlarm 分级报警设置
     * @return 结果
     */
    @Override
    public int updateGradedAlarm(GradedAlarm gradedAlarm) {
        gradedAlarm.setUpdateTime(DateUtils.getNowDate());
        return gradedAlarmMapper.updateGradedAlarm(gradedAlarm);
    }

    /**
     * 批量删除分级报警设置
     *
     * @param gradedAlarmIds 需要删除的分级报警设置主键
     * @return 结果
     */
    @Override
    public int deleteGradedAlarmByGradedAlarmIds(String[] gradedAlarmIds) {
        return gradedAlarmMapper.deleteGradedAlarmByGradedAlarmIds(gradedAlarmIds);
    }

    /**
     * 删除分级报警设置信息
     *
     * @param gradedAlarmId 分级报警设置主键
     * @return 结果
     */
    @Override
    public int deleteGradedAlarmByGradedAlarmId(String gradedAlarmId) {
        return gradedAlarmMapper.deleteGradedAlarmByGradedAlarmId(gradedAlarmId);
    }
}
